﻿using System;
using System.Drawing;
using System.ComponentModel;

namespace MossmanMill.View
{
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(ChannelSelect))]
    public class ChannelSelect : System.Windows.Forms.Panel
    {

        private int channels, selected, scaleX, scaleY;
        private Brush onBrush, offBrush, labelBrush;
        private Font font;
        private bool[] hasSensor;

        public int Channel
        {
            get { return selected; }
            set { if (value > channels) selected = value; }
        }

        /// <summary>
        /// Specifies the color for the selected channel
        /// </summary>
        [Bindable(true), Category("Color Options"),
        Description("The color for the selected channel")]
        public System.Drawing.Brush OnBrush
        {
            get { return onBrush; }
            set
            {
                onBrush = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// Specifies the color for non selected channels
        /// </summary>
        [Bindable(true), Category("Color Options"),
        Description("The color for all other channels")]
        public System.Drawing.Brush OffBrush {
            get { return offBrush; }
            set {
                offBrush = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// Specifies the color for the channel labels
        /// </summary>
        [Bindable(true), Category("Color Options"),
        Description("The color for the channel label")]
        public System.Drawing.Brush LabelBrush
        {
            get { return labelBrush; }
            set
            {
                labelBrush = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// The event called when a slot is selected.
        /// </summary>
        [Bindable(true), Category("ChannelSelect"),
        Description("Called when clicked")]
        public event EventHandler<EventArgs> ChannelSelected;

        public ChannelSelect()
        {
            channels = 16;
            font = new Font("Arial", 8);
            selected = 0;
            onBrush = Brushes.Aqua;
            offBrush = Brushes.Linen;
            labelBrush = Brushes.Black;
            scaleX = scaleY = 20;
            hasSensor = new bool[channels];
            ChannelSelected += SelectSensor;
        }

        private void SelectSensor(object sender, EventArgs e)
        {
        }        

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);
            scaleX = Width / 4;
            scaleY = Height / 8;
            System.Drawing.Graphics g = e.Graphics;
            //g.DrawString("Channel Select", font, Brushes.Black, 0, 0);
            if (channels <= 0) return;
            int count = 0;
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 8; x++)
                {
                    var brush = selected == count ? onBrush : offBrush;
                    g.FillRectangle(brush, y * scaleX, x * scaleY, scaleX, scaleY);
                    g.DrawRectangle(new Pen(labelBrush), y * scaleX, x * scaleY, scaleX, scaleY);
                    g.DrawString("" + count, font, labelBrush, y * scaleX, x * scaleY);
                    if (hasSensor[count]) g.FillEllipse(Brushes.Maroon, y * scaleX + 12, x * scaleY + 12, 8, 8);
                    count++;
                    if (count == channels) break;
                }
                if (count == channels) break;
            }
            
        }

        protected override void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
        {
            scaleX = Width / 4;
            scaleY = Height / 8;
            var rect = new System.Drawing.Rectangle(0, 0, Width, Height);
            if (rect.Contains(e.X, e.Y))
            {
                int x = e.X / scaleX;
                int y = e.Y / scaleY;
                int ch = x * 8 + y;
                if (ch >= channels) return;
                selected = ch;
                ChannelSelected(this, EventArgs.Empty);
            }
            Invalidate();
        }

        public void setNumChannels(int ch)
        {
            var sensors = new bool[ch];
            channels = ch;
            hasSensor = sensors;
            Invalidate();
        }

        public void setHasSensor(int ch, bool sensor)
        {
            if (ch > channels - 1) return;
            if (ch < 0) return;
            hasSensor[ch] = sensor;
            Invalidate();
        }
    }
}
